package com.dcjt.firehotcarpad.utils

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import android.util.Log
import java.io.*
import java.text.SimpleDateFormat
import java.util.*


class CrashHandler:Thread.UncaughtExceptionHandler {
    companion object {

        private var mDefaultCrashHandler: Thread.UncaughtExceptionHandler? = null
        private const val FILE_NAME = "Command_crash"
        private const val FILE_NAME_SUFEIX = ".txt"
        private val mCrashHandler = CrashHandler()

        //文件路径
        private val PATH = Environment.getExternalStorageDirectory().path + File.separator + "Command_Car_crash"

        @JvmStatic
        fun getInstance() = mCrashHandler
    }

    private constructor()

    private lateinit var mContext: Context

    fun init(context: Context) {
        mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler()
        Thread.setDefaultUncaughtExceptionHandler(this)
        mContext = context.applicationContext
    }

    override fun uncaughtException(thread: Thread, ex: Throwable) {
        ex.printStackTrace()
        try {
            //将文件写入sd卡
            writeToSDcard(ex)
            //写入后在这里可以进行上传操作
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }

        //如果系统提供了默认异常处理就交给系统进行处理，否则自己进行处理。
        if (mDefaultCrashHandler != null) {
            mDefaultCrashHandler?.uncaughtException(thread, ex)
        } else {
            //Process.killProcess(Process.myPid());
            android.os.Process.killProcess(android.os.Process.myPid())
        }
    }


    //将异常写入文件
    @Throws(IOException::class, PackageManager.NameNotFoundException::class)
    private fun writeToSDcard(ex: Throwable) {
        //如果没有SD卡，直接返回
        if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) {
            return
        }
        val dir = File(PATH)
        if (!dir.exists()) {
            dir.mkdirs()
        }
        val currentTime = System.currentTimeMillis()
        val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE).format(
            Date(currentTime)
        )

        val exFile = File(PATH + File.separator + FILE_NAME  + FILE_NAME_SUFEIX)
        val pw = PrintWriter(BufferedWriter(FileWriter(exFile)))
        Log.e("错误日志文件路径", "" + exFile.absolutePath)
        pw.println(time)
        val pm = mContext.getPackageManager()
        val pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES)
        //当前版本号
        pw.println("App Version:" + pi.versionName + "_" + pi.versionCode)
        //当前系统
        pw.println("OS version:" + Build.VERSION.RELEASE + "_" + Build.VERSION.SDK_INT)
        //制造商
        pw.println("Vendor:" + Build.MANUFACTURER)
        //手机型号
        pw.println("Model:" + Build.MODEL)
        //CPU架构
        pw.println("CPU ABI:" + Build.CPU_ABI)

        ex.printStackTrace(pw)
        pw.close()

    }
}
